﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Net;
using System.Net.NetworkInformation;
using System.Net.Sockets;
using Microsoft.AspNet.SignalR.Client;

namespace AisinGioro.ConsoleTest
{
    class Program
    {
        static void Main(string[] args)
        {
            Program prog = new Program();
            prog.StartToConnect();

            //Core.NetworkInfo ni = new Core.NetworkInfo();
            //string message;
            //bool result = ni.HttpGet("https://www.6008871.com", out message);
            //Console.WriteLine(message);

            //ni.SocketConnect("52.68.138.46", 3389, out message);
            //Console.WriteLine(message);

            //ni.Ping("52.199.166.244", out message);
            //Console.WriteLine(message);

            Console.WriteLine("Completed");
            Console.ReadLine();
        }

        HubConnection _hubConn;
        IHubProxy _proxy;
        void StartToConnect()
        {
            _hubConn = new HubConnection("http://localhost:9000/signalr", "role=host");
            _hubConn.TransportConnectTimeout = new TimeSpan(0, 0, 10);
            _hubConn.StateChanged += HubConnStateChanged;
            //_hubConn.TraceLevel = TraceLevels.All;
            //_hubConn.TraceWriter = Console.Out;
            _proxy = _hubConn.CreateHubProxy("msgHub");
            _proxy.On<string>("broadcast", (msg) =>
            {
                Console.WriteLine("收到服务端的问候：{0}", msg);
            });
            _proxy.On<string>("receiveNotify", (msg) =>
            {
                Console.WriteLine("收到服务端的回复：{0}", msg);
            });

            try
            {
                _hubConn.Start().Wait();

                cancelTokenSource = new CancellationTokenSource();
                LoopKeepAliveAsync();
            }
            catch (Exception ex)
            {
                while (ex.InnerException != null)
                    ex = ex.InnerException;

                Console.WriteLine(ex.Message);
            }
        }

        CancellationTokenSource cancelTokenSource;
        async void LoopKeepAliveAsync()
        {            
            while (true)
            {
                if (cancelTokenSource.IsCancellationRequested) break;
                if (_hubConn.State == ConnectionState.Connected)
                {
                    Console.WriteLine("调用服务端方法 KeepAlive");
                    await _proxy.Invoke("keepAlive");
                }
                await Task.Delay(2000);
            }
        }
        
        void HubConnStateChanged(StateChange stateChange)
        {
            switch (stateChange.NewState)
            {
                case ConnectionState.Connecting:
                    Console.WriteLine("Connecting to server..."); break;
                case ConnectionState.Connected:
                    Console.WriteLine("Connected"); break;
                case ConnectionState.Reconnecting:
                    Console.WriteLine("Reconnecting to server..."); break;
                case ConnectionState.Disconnected:
                    {
                        if (stateChange.OldState == ConnectionState.Reconnecting)
                            Console.WriteLine("Reconnect failed, abort");

                        Console.WriteLine("Disconnected");
                    }
                    break;
            }
        }
    }
}
